<template>
  <el-dialog v-model="dialogFormVisible" :title='props.deleteTitle' width="30%">
    <span v-html="props.deleteTips"></span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="confirmDelete">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { deleteMessageFirstAPI, deleteMessageAPI, restoreMessageAPI, getMessageByIdAPI } from '../../../api/message.js'
import { ElMessage } from 'element-plus'
import { tracking } from '../../../utils/operate'
import { deleteDepartmentMsgAPI, addDepartmentMsgAPI } from '../../../api/department_msg'



// 消息ID
const messageId = ref()
const messageTitle = ref()

const props = defineProps<{
  getCompanyMessageByPager?: Function,
  getSystemMessageByPager?: Function,
  getRecycleBinByPager?: Function,
  deleteTitle: string,
  deleteTips: string
}>()

const department = JSON.parse(localStorage.getItem('userinfo') as string)?.department

const confirmDelete = async () => {
  if (props.deleteTitle == '删除公司公告' || props.deleteTitle == '删除系统消息') {
    const res = await deleteMessageFirstAPI({ id: messageId.value })
    if (res.data.status == 0) {
      ElMessage({
        message: '删除成功',
        type: 'success',
      })
      deleteDepartmentMsgAPI({ readId: messageId.value, department: department })
      props.getCompanyMessageByPager?.()
      props.getSystemMessageByPager?.()
      dialogFormVisible.value = false
      tracking("消息管理", `${props.deleteTitle}`, { title: messageTitle.value }, "中级")
    } else {
      ElMessage.error('删除失败')
      dialogFormVisible.value = false
    }
  } else if (props.deleteTitle == '真正删除消息') {
    const res = await deleteMessageAPI({ id: messageId.value })
    if (res.data.status == 0) {
      deleteDepartmentMsgAPI({ readId: messageId.value, department: department })
      ElMessage({
        message: '删除成功',
        type: 'success',
      })
      props.getRecycleBinByPager?.()
      dialogFormVisible.value = false
      tracking("消息管理", `永久删除了消息`, { title: messageTitle.value }, "中级")
    } else {
      ElMessage.error('删除失败')
      dialogFormVisible.value = false
    }
  } else if (props.deleteTitle == '还原此条消息') {
    const res = await restoreMessageAPI({ id: messageId.value })
    if (res.data.status == 0) {
      ElMessage({
        message: '还原成功',
        type: 'success',
      })
      
      await addDepartmentMsgAPI({ readId: messageId.value, department: department })
      props.getRecycleBinByPager?.()
      dialogFormVisible.value = false
      tracking("消息管理", `还原了消息`, { title: messageTitle.value }, "中级")
    } else {
      ElMessage.error('还原失败')
      dialogFormVisible.value = false
    }
  }
}

// 弹窗开关
const dialogFormVisible = ref(false)

// 打开删除管理员的弹窗
const open = (id, msgTitle) => {
  messageId.value = id
  messageTitle.value = msgTitle
  dialogFormVisible.value = true
}

defineExpose({
  open
})

</script>

<style lang="scss" scoped></style>